# usage: python computeAveragesAndStdErrorsZconstraintData_2sets.py

import sys,string,math

# Avg and SE for vals from force.dat
f1 = open('analysisSet1/force.dat','r')
f2 = open('analysisSet2/force.dat','r')
f3 = open('analysisSet3/force.dat','r')
f4 = open('analysisSet4/force.dat','r')
o = open('force.dat','w') # Write file with all three values of the constraint force.
while 1: # Create infinite loop
        line1 = f1.readline() # Read each line - data MUST begin at line 0.
        line2 = f2.readline()
	line3 = f3.readline()
        line4 = f4.readline()
	if not line1: break # Break reading loop if blank line
        if not line2: break
        if not line3: break
        if not line4: break
        words1 = string.split(line1) # Split each line into columns
        words2 = string.split(line2)
        words3 = string.split(line3)
        words4 = string.split(line4)
        print >> o, words1[0], words1[1], words2[1], words3[1], words4[1] # Print each value
f1.close()
f2.close()
f3.close()
f4.close()
o.close()

f = open('force.dat','r')
o = open('avgForce.dat','w')
for line in f:
	val = line.split()
	x = float(val[0]);
	force1 = float(val[1]);
	force2 = float(val[2]);
	force3 = float(val[3]);
	force4 = float(val[4]);
	sumy = force1 + force2 + force3 + force4 
	#print >>o, sumy
	avgy = float(sumy/4) # Avgerage values.
	#av = float((a-avgy)**2) #Calculate the square of the distance from the mean for each value
	#bv = float((b-avgy)**2)
	#cv = float((c-avgy)**2)
	sqForce1 = float((force1)**(2))
	sqForce2 = float((force2)**(2))
	sqForce3 = float((force3)**(2))
	sqForce4 = float((force4)**(2))
	sum2 = float((sqForce1+sqForce2+sqForce3+sqForce4))
	sumsq = float(((force1+force2+force3+force4)**(2))/4)
	#sdy = float(((av+bv+cv)/3)**(1/2.0))
	sdy = float(((sum2-sumsq)/2)**(1/2.0))
	sey = float((sdy)/((2)**(1/2.0)))
	print >>o, x,"\t",avgy,"\t",sey
	#print >>o, x,"\t",round(avgy,3)," \t",round(sey,3) Rounding - if req.
f.close()
o.close()

# Avg vals and SE for diffusion coefficient.
d1 = open('analysisSet1/D.dat','r')
d2 = open('analysisSet2/D.dat','r')
d3 = open('analysisSet3/D.dat','r')
d4 = open('analysisSet4/D.dat','r')
o = open('D.dat','w')
while 1:
        line1 = d1.readline() # Read each line - data MUST begin at line 0.
        line2 = d2.readline()
	line3 = d3.readline()
        line4 = d4.readline()
	if not line1: break # Break reading loop if blank line
        if not line2: break
        if not line3: break
        if not line4: break
        words1 = string.split(line1) # Split each line into columns
        words2 = string.split(line2)
        words3 = string.split(line3)
        words4 = string.split(line4)
        print >> o, words1[0], words1[1], words2[1], words3[1], words4[1] # Print each value
d1.close()
d2.close()
d3.close()
d4.close()
o.close()

f = open('D.dat','r')
o = open('avgD.dat','w')
for line in f:
	val = line.split()
	x = float(val[0]);
	diff1 = float(val[1]);
	diff2 = float(val[2]);
	diff3 = float(val[3]);
	diff4 = float(val[4]);
	sumy = diff1 + diff2 + diff3 + diff4 
	avgy = float(sumy/4) # Avgerage values.
	sqDiff1 = float((diff1)**(2))
	sqDiff2 = float((diff2)**(2))
	sqDiff3 = float((diff3)**(2))
	sqDiff4 = float((diff4)**(2))
	sum2 = float((sqDiff1+sqDiff2+sqDiff3+sqDiff4))
	sumsq = float(((diff1+diff2+diff3+diff4)**(2))/4)
	sdy = float(((sum2-sumsq)/2)**(1/2.0))
	sey = float((sdy)/((2)**(1/2.0)))
	print >>o, x,"\t",avgy,"\t",sey
f.close()
o.close()

# Avg vals and SE for free energy.
g1 = open('analysisSet1/G.dat','r')
g2 = open('analysisSet2/G.dat','r')
g3 = open('analysisSet3/G.dat','r')
g4 = open('analysisSet4/G.dat','r')
o = open('G.dat','w')
while 1:
        line1 = g1.readline() # Read each line - data MUST begin at line 0.
        line2 = g2.readline()
	line3 = g3.readline()
        line4 = g4.readline()
	if not line1: break # Break reading loop if blank line
        if not line2: break
        if not line3: break
        if not line4: break
        words1 = string.split(line1) # Split each line into columns
        words2 = string.split(line2)
        words3 = string.split(line3)
        words4 = string.split(line4)
        print >> o, words1[0], words1[1], words2[1], words3[1], words4[1] # Print each value
g1.close()
g2.close()
g3.close()
g4.close()
o.close()

f = open('G.dat','r')
o = open('avgG.dat','w')
for line in f:
	val = line.split()
	x = float(val[0]); 
	deltaG1 = float(val[1]);
	deltaG2 = float(val[2]);
	deltaG3 = float(val[3]);
	deltaG4 = float(val[4]);
	sumy = deltaG1 + deltaG2 + deltaG3 + deltaG4 
	avgy = float(sumy/4) # Avgerage values.
	sqDeltaG1 = float((deltaG1)**(2))
	sqDeltaG2 = float((deltaG2)**(2))
	sqDeltaG3 = float((deltaG3)**(2))
	sqDeltaG4 = float((deltaG4)**(2))
	sum2 = float((sqDeltaG1+sqDeltaG2+sqDeltaG3+sqDeltaG4))
	sumsq = float(((deltaG1+deltaG2+deltaG3+deltaG4)**(2))/4)
	sdy = float(((sum2-sumsq)/2)**(1/2.0))
	sey = float((sdy)/((2)**(1/2.0)))
	print >>o, x,"\t",avgy,"\t",sey
f.close()
o.close()

# Avg vals and SE for resistance.
r1 = open('analysisSet1/R.dat','r')
r2 = open('analysisSet2/R.dat','r')
r3 = open('analysisSet3/R.dat','r')
r4 = open('analysisSet4/R.dat','r')
o = open('R.dat','w')
while 1:
        line1 = r1.readline() # Read each line - data MUST begin at line 0.
        line2 = r2.readline()
	line3 = r3.readline()
        line4 = r4.readline()
	if not line1: break # Break reading loop if blank line
        if not line2: break
        if not line3: break
        if not line4: break
        words1 = string.split(line1) # Split each line into columns
        words2 = string.split(line2)
        words3 = string.split(line3)
        words4 = string.split(line4)
        print >> o, words1[0], words1[1], words2[1], words3[1], words4[1] # Print each value
r1.close()
r2.close()
r3.close()
r4.close()
o.close()

f = open('R.dat','r')
o = open('avgR.dat','w')
for line in f:
	val = line.split()
	x = float(val[0]);
	res1 = float(val[1]);
	res2 = float(val[2]);
	res3 = float(val[3]);
	res4 = float(val[4]);
	sumy = res1 + res2 + res3 + res4 
	avgy = float(sumy/4) # Avgerage values.
	sqDeltaG1 = float((res1)**(2))
	sqDeltaG2 = float((res2)**(2))
	sqDeltaG3 = float((res3)**(2))
	sqDeltaG4 = float((res4)**(2))
	sum2 = float((sqDeltaG1+sqDeltaG2+sqDeltaG3+sqDeltaG4))
	sumsq = float(((res1+res2+res3+res4)**(2))/4)
	sdy = float(((sum2-sumsq)/2)**(1/2.0))
	sey = float((sdy)/((2)**(1/2.0)))
	print >>o, x,"\t",avgy,"\t",sey
f.close()
o.close()

# Calculate average vals and SE for the permeability coefficient.
f = open('analysisSet1/P','r')
o = open('avgP.dat','w')
while 1:
  line = f.readline()
  if not line:  break
  words = string.split(line)
  if 'Permeability' in line:
    if 'cm' in line:
        p1 = float(words[4])
f.close()
f = open('analysisSet2/P','r')
while 1:
  line = f.readline()
  if not line:  break
  words = string.split(line)
  if 'Permeability' in line:
    if 'cm' in line:
        p2 = float(words[4])
f.close()
f = open('analysisSet3/P','r')
while 1:
  line = f.readline()
  if not line:  break
  words = string.split(line)
  if 'Permeability' in line:
    if 'cm' in line:
        p3 = float(words[4])
f.close()
f = open('analysisSet4/P','r')
while 1:
  line = f.readline()
  if not line:  break
  words = string.split(line)
  if 'Permeability' in line:
    if 'cm' in line:
        p4 = float(words[4])
f.close()

sum = p1 + p2 + p3 + p4
avg = sum/4
sqP1 = float((p1)**(2))
sqP2 = float((p2)**(2))
sqP3 = float((p3)**(2))
sqP4 = float((p4)**(2))
sum2 = float((sqP1+sqP2+sqP3+sqP4))
sumsq = float(((p1+p2+p3+p4)**(2))/4)
sd = float(((sum2-sumsq)/2)**(1/2.0))
se = float((sd)/((2)**(1/2.0)))
print >>o, "%8.3e"% avg,"%8.3e"% se
f.close()
o.close()



